<template>
  <el-dialog
  :title="title"
  :visible.sync="visible"
  width="40%"
  :before-close="handleClose">
    <el-form :model="formData" ref="formData" :rules="rules" label-width="100px" class="demo-ruleForm" style="width:90%">
    <el-form-item label="类型：" prop="type">
    <el-radio-group   el-radio-group v-model="formData.type">
        <el-radio :label="1">目录</el-radio>
        <el-radio :label="2">菜单</el-radio>
        <el-radio :label="3" v-if="formData.parentId">按钮</el-radio>
    </el-radio-group>
    </el-form-item>
    <el-form-item label="名称：" prop="name">
        <el-input v-model="formData.name"></el-input>
    </el-form-item>
    <el-form-item label="权限标识：" prop="code">
        <el-input v-model="formData.code"></el-input>
    </el-form-item>
    <el-form-item label="请求地址：
" prop="url" v-if="formData.type!==3">
        <el-input v-model="formData.url"></el-input>
    </el-form-item>
    <el-form-item label="图标：" prop="icon" v-if="formData.type!==3">
        <el-input v-model="formData.icon"></el-input>
    </el-form-item>
    <el-form-item label="排序：" prop="sort">
        <el-input-number v-model="formData.sort" :min="1" :max="100"></el-input-number>
    </el-form-item>
    <el-form-item label="备注：" prop="remark">
        <el-input type="textarea" v-model="formData.remark"></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" size="mini" @click="submitForm('formData')">确定</el-button>
        <el-button @click="handleClose" size="mini">取消</el-button>
    </el-form-item>
    </el-form>
    </el-dialog>
</template>

<script>
import api from '@/api/menu'

export default {
    data() {
        return {
            rules: {
                type: [
                    { required: true, message: '请输入类型', trigger: 'change' }                    
                ],
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }                    
                ],
                code: [
                    { required: true, message: '请输入权限标识', trigger: 'blur' }                    
                ],
            }
        }
    },
    props:{
        visible:{
            type:Boolean,
            default:false
        },
        title:{
            type:String,
            default:''
        },
        formData:{
            type:Object,
            default:{}
        },
        remoteClose: Function 
    },
    methods: {
        // 关闭窗口
        handleClose(){
            this.$refs.formData.resetFields()
            this.remoteClose()
        },
        // 提交表格，验证
        submitForm(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if(this.formData.type===3){
                        this.formData.url=''
                        this.formData.icon=''                       
                    }
                    this.submitData()
                } else {
                    this.$message({
                    message: '保存失败',
                    type: 'error'
                    });
                    return false;
                }
            });
        },
        // 提交数据，检验
        async submitData(){
            let response=null

            if(this.formData.id){
                response = await api.update(this.formData)

            }else{
                response = await api.add(this.formData)
            }
            if (response.code === 20000){
                this.$message({
                    message: '保存成功',
                    type: 'success'
                    });
                this.handleClose()
                
            }else{
                this.$message({
                    message: '保存失败',
                    type: 'error'
                    });
            }
        }
    },
}
</script>

<style>

</style>